<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="handleOk">查询</a-button>
              <a-button style="margin-left: 8px" @click="queryParamReset">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.createModal.add()">添加</a-button>
    </div>

    <table-ext
      ref="table"
      size="default"
      bordered
      rowKey="id"
      :columns="columns"
      :data="loadData"
      showPagination="auto"
    >
      <span slot="avatar" slot-scope="avatar">
        <a-avatar :src="avatar" />
      </span>
      <span slot="status" slot-scope="text">
        <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
      </span>
    </table-ext>

    <create-form ref="createModal" @ok="handleOk" />
  </a-card>
</template>

<script>
import { TableExt } from '@/components'
import { settlementAmountList } from '@/api/shop/settlementAmount'
import CreateForm from './modules/CreateForm'

const statusMap = {
  0: {
    status: 'error',
    text: '禁用'
  },
  1: {
    status: 'success',
    text: '正常'
  }
}

export default {
  name: 'TableList',
  components: {
    TableExt,
    CreateForm
  },
  data () {
    return {
      description: '',

      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParams: [

      ],
      initQueryParams: [],
      // 表头
      columns: [
        {
          title: 'ID',
          align: 'center',
          dataIndex: 'id'
        },
        {
          title: '结算金额',
          align: 'center',
          dataIndex: 'amount'
        },
        {
          title: '结算日期',
          align: 'center',
          dataIndex: 'settlementDate'
        },
        {
          title: '创建时间',
          align: 'center',
          dataIndex: 'gmtCreate'
        },
        {
          title: '更新时间',
          align: 'center',
          dataIndex: 'gmtModified'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return settlementAmountList({ ...parameter, queryParams: this.queryParams }).then(res => {
          return res.data
        })
      }
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  created () {
    this.initQueryParams = JSON.parse(JSON.stringify(this.queryParams))
  },
  methods: {
    handleOk () {
      this.$refs.table.refresh()
    },
    handleEdit (record) {
      this.$refs.editModal.edit(record)
    },
    queryParamReset () {
      this.queryParams = JSON.parse(JSON.stringify(this.initQueryParams))
      this.handleOk()
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  }
}
</script>
